package vn.bk.hotel.utils;

import java.util.ArrayList;
import java.util.List;

import com.google.android.gms.maps.model.LatLng;

public class MapUtils {

    public static String makeURL(double sourcelat, double sourcelog,
	    double destlat, double destlog) {
	StringBuilder urlString = new StringBuilder();
	urlString.append("http://maps.googleapis.com/maps/api/directions/json");
	urlString.append("?origin=");// from
	urlString.append(Double.toString(sourcelat));
	urlString.append(",");
	urlString.append(Double.toString(sourcelog));
	urlString.append("&destination=");// to
	urlString.append(Double.toString(destlat));
	urlString.append(",");
	urlString.append(Double.toString(destlog));
	urlString.append("&sensor=false&mode=driving&alternatives=true");
	return urlString.toString();
    }

    public static List<LatLng> decodePoly(String encoded) {

	List<LatLng> poly = new ArrayList<LatLng>();
	int index = 0, len = encoded.length();
	int lat = 0, lng = 0;

	while (index < len) {
	    int b, shift = 0, result = 0;
	    do {
		b = encoded.charAt(index++) - 63;
		result |= (b & 0x1f) << shift;
		shift += 5;
	    } while (b >= 0x20);
	    int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
	    lat += dlat;

	    shift = 0;
	    result = 0;
	    do {
		b = encoded.charAt(index++) - 63;
		result |= (b & 0x1f) << shift;
		shift += 5;
	    } while (b >= 0x20);
	    int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
	    lng += dlng;

	    LatLng p = new LatLng((((double) lat / 1E5)),
		    (((double) lng / 1E5)));
	    poly.add(p);
	}

	return poly;
    }
}
